namespace Aphalina.Compression
{
    using System;
    using System.IO;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;

    public class CompressStream : Stream
    {
        private Adler32 adler;
        private Deflate deflate;
        private Stream stream;

        public CompressStream(Stream stream, bool isZLib = false)
        {
            this.stream = stream;
            this.IsZLib = isZLib;
            this.deflate = new Deflate(stream);
            if (this.IsZLib)
            {
                stream.WriteByte(120);
                stream.WriteByte(0xda);
                this.adler = new Adler32();
            }
        }

        private void CheckStream()
        {
            if (this.stream == null)
            {
                throw new InvalidOperationException();
            }
        }

        public override void Close()
        {
            if (this.stream != null)
            {
                this.deflate.Finish();
                if (this.IsZLib)
                {
                    this.stream.WriteByte((byte) (this.adler.Value >> 0x18));
                    this.stream.WriteByte((byte) ((this.adler.Value >> 0x10) & 0xff));
                    this.stream.WriteByte((byte) ((this.adler.Value >> 8) & 0xff));
                    this.stream.WriteByte((byte) (this.adler.Value & 0xff));
                }
                this.deflate = null;
                this.stream = null;
            }
        }

        public override void Flush()
        {
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            throw new InvalidOperationException();
        }

        public override long Seek(long offset, SeekOrigin origin)
        {
            throw new InvalidOperationException();
        }

        public override void SetLength(long value)
        {
            throw new InvalidOperationException();
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            this.CheckStream();
            if (this.IsZLib)
            {
                this.adler.Update(buffer, offset, count);
            }
            this.deflate.Encode(buffer, offset, count);
        }

        public override bool CanRead
        {
            get
            {
                return false;
            }
        }

        public override bool CanSeek
        {
            get
            {
                return false;
            }
        }

        public override bool CanWrite
        {
            get
            {
                return true;
            }
        }

        public bool IsZLib { get; private set; }

        public override long Length
        {
            get
            {
                throw new InvalidOperationException();
            }
        }

        public override long Position
        {
            get
            {
                throw new InvalidOperationException();
            }
            set
            {
                throw new InvalidOperationException();
            }
        }
    }
}

